﻿using Inovout.Models;
using Inovout.Runtime;
using Inovout.Security;
using System;

namespace Inovout.Memberships.Models
{
    public partial class MembershipUser : IMembershipUser
    {
        public virtual string Name
        {
            get
            {
                return string.Format(nameFormat, this.IdentifierCode);
            }
        }
        private static readonly string nameFormat;
        static MembershipUser()
        {
            nameFormat = ComponentRegistry.Resolve<IApplication>().Settings["MembershipUserIdentityFormat"].ToString();

        }

        public MembershipUser(IMembership membership, ISystemUser systemUser, string email, MembershipUserStatus status)
        {
            this.Membership = membership;
            this.SystemUser = systemUser as SystemUser;
            this.IdentifierCode = NewGuid();
            this.Email = email;

            this.Status = status;
        }


        ISystemUser IMembershipUser.SystemUser
        {
            get { return systemUser; }
        }

        //当hadoop版本升级时，去除此方法
        private string NewGuid()
        {
            var guid = string.Empty;
            char[] constant =  {   
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'  
      };
            Random rand = new Random();
            char randChar = constant[rand.Next(26)];
            guid = Guid.NewGuid().ToString("N");
            var firstChar = guid.ToString().ToCharArray()[0];
            if (firstChar <= '9' && firstChar >= '0')
            {
                guid = randChar + guid.Substring(1, guid.Length - 2);
            }
            return guid;
        }
    }
}
